{extend name="common/base" /}
{block name="body"}
<div class="search-block">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">登录时间</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="last_time" placeholder="请选择用户的登录时间段">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="title" placeholder="请输入用户名" id="title" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <button class="layui-btn layui-btn-sm" id="search_btn">立即搜索</button>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="layui-tab-content" style="background-color: rgba(255,255,255,1);margin-top: 5px;">
    <div class="layui-tab-item layui-show">
        <div class="layui-btn-group demoTable" style="float: left;">
            <button class="layui-btn layui-btn-sm" id="created">添加管理员</button>
            <button class="layui-btn layui-btn-sm" id="delete">批量删除</button>
        </div>
        <span class="layui-clear"></span>
        <table class="layui-hide" id="table_list" lay-filter="lists"></table>
    </div>
</div>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius" lay-event="modify">编辑用户</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-radius" lay-event="delete">删除用户</a>
</script>
{/block}
{block name="script"}
<script>
    var table;
    layui.config({
        base: '__ADMIN__/modules/'
    }).use(['layer', 'element', 'table', 'jquery', 'laydate', 'home'], function () {
        var layer = layui.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            form = layui.form,
            home = layui.home,
            element = layui.element;
        table = layui.table;
        //日期
        //日期时间范围
        laydate.render({
            elem: '#last_time'
            ,type: 'datetime'
            ,range: true
        });
        //保存当前页
        var page = 1;
        var limit = home.fitPage();
        //方法级渲染
        table.render({
            elem: '#table_list'
            , url: "{:url('Admin/lists')}"
            , cols: [[
                {checkbox: true, fixed: true}
                , {field: 'id', title: 'ID', width: "5%"}
                , {field: 'name', title: '登录名', width: "30%", edit: "text"}
                , {field: 'last_time', title: '最后登录时间', width: "20%", edit: "text"}
                , {field: 'ip', title: '最后登录IP', width: "30%", edit: "text"}
                , {fixed: 'right', width: "15%", align: 'center', toolbar: '#tool'}
            ]]
            , id: 'lists'
            , page: true
            , limit: limit
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , statusCode: 200 //成功的状态码，默认：0
                , msgName: 'msg' //状态信息的字段名称，默认：msg
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //数据列表的字段名称，默认：data
            }, done: function (res, curr, count) {
                page = curr;
            }
        });
        //监听单元格编辑
        table.on('edit(lists)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            $.ajax({
                type: "POST",
                async: true,
                url: "{:url('AuthGroup/modify')}",
                data: data,
                dataType: "json",
                success: function (res) {
                    if (res && res['status'] == 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
        });
        $("#search_btn").click(function () {
            //名称
            var name = $("#name").val();
            var title = $("#title").val();
            var ismenu = $("#ismenu").val();
            //执行重载
            table.reload('lists', {
                page: {
                    curr: page //重新从第 1 页开始
                }
                , where: {
                    name: name,
                    title: title,
                    ismenu: ismenu
                }
            });
            return false;
        });
        //监听工具条
        table.on('tool(lists)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'modify') { //编辑
                layer.open({
                    title: "编辑管理员【"+data.name+"】",
                    type: 2,
                    area: ['400px', '280px'],
                    content: "{:url('Admin/modify')}"+"?id="+data.id
                });
            } else if (layEvent === 'delete') { //删除
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "post",
                        async: true,
                        url: "{:url('Admin/delete')}",
                        data: {id: data.id},
                        dataType: "json",
                        success: function (res) {
                            if (res && res['status'] == 200) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'create-children') {
                layer.open({
                    title: "添加"+data.title+"的子菜单菜单",
                    type: 2,
                    area: ['400px', '400px'],
                    content: "{:url('Menus/created')}"+"?pid="+data.id,
                    success: function(layero, index){
                        console.log(layero, index);
                    }
                });
            }
        });
        //添加
        $("#created").click(function () {
            layer.open({
                title: "添加管理员",
                type: 2,
                area: ['400px', '280px'],
                content: "{:url('Admin/created')}"
            });
        });
        //全选删除
        $("#delete").click(function () {
            var checkStatus = table.checkStatus('lists')
                ,data = checkStatus.data;
            var ids = "";
            $.each(data,function (x,y) {
                ids+=","+y.id;
            })
            layer.confirm('数据无价、请谨慎操作？', function (index) {
                $.ajax({
                    type: "post",
                    async: true,
                    url: "{:url('Admin/delete')}",
                    data: {id: ids},
                    dataType: "json",
                    success: function (res) {
                        if (res && res['status'] == 200) {
                            table.reload('lists', {
                                page: {
                                    curr: page
                                }
                            });
                            layer.close(index);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
            });
        })
    });
</script>
{/block}